package com.sinothk.fc.store.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinothk.base.serialNumber.manager.SerialNumberHelper;
import com.sinothk.fc.browser.entity.BrowserEntity;
import com.sinothk.fc.browser.mapper.BrowserMapper;
import com.sinothk.fc.collect.entity.CollectEntity;
import com.sinothk.fc.collect.service.ICollectService;
import com.sinothk.fc.employee.service.IStoreEmployeeService;
import com.sinothk.fc.goods.entity.GoodsEntity;
import com.sinothk.fc.goods.service.IGoodsService;
import com.sinothk.fc.store.entity.StoreEntity;
import com.sinothk.fc.store.entity.StoreHotGoodsVO;
import com.sinothk.fc.store.entity.StoreVO;
import com.sinothk.fc.store.mapper.FcStoreMapper;
import com.sinothk.fc.store.service.IFcStoreService;
import com.sinothk.fc.storeNotice.service.IStoreNoticeService;
import com.sinothk.fc.storeSupplier.entity.StoreSupplierEntity;
import com.sinothk.fc.storeSupplier.service.IStoreSupplierService;
import com.sinothk.special.AppConstant;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description: 招财猫门店
 * @Author: Liangyt
 * @Date: 2022-07-10
 * @Version: V1.0
 */
@Service
public class FcStoreServiceImpl extends ServiceImpl<FcStoreMapper, StoreEntity> implements IFcStoreService {
    private final ICollectService collectService;
    @Resource
    private FcStoreMapper storeMapper;
    @Resource
    private BrowserMapper browserMapper;
    private final IStoreNoticeService storeNoticeService;
    private final IStoreEmployeeService storeEmployeeService;
    private final IGoodsService goodsService;
    private final IStoreSupplierService storeSupplierService;

    public FcStoreServiceImpl(IStoreNoticeService storeNoticeService, IStoreEmployeeService storeEmployeeService, IGoodsService goodsService, ICollectService collectService, IStoreSupplierService storeSupplierService) {
        this.storeNoticeService = storeNoticeService;
        this.storeEmployeeService = storeEmployeeService;
        this.goodsService = goodsService;
        this.collectService = collectService;
        this.storeSupplierService = storeSupplierService;
    }

    @Override
    public StoreEntity queryStoreAllInfoByStoreId(String storeId) {
        return storeMapper.queryStoreAllInfoByStoreId(storeId);
    }

    @Override
    public StoreEntity initStore() {
        StoreEntity fcStore = new StoreEntity();
        fcStore.setStoreNo(SerialNumberHelper.getSerialNumber(AppConstant.BizType.STORE));
        return fcStore;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public StoreEntity addOrEdit(StoreEntity fcStore) throws Exception {
        if (StringUtils.isEmpty(fcStore.getId())) {
            fcStore.setStoreNo(StrUtil.isNotBlank(fcStore.getStoreNo()) ? fcStore.getStoreNo() : SerialNumberHelper.getSerialNumber(AppConstant.BizType.STORE));
            fcStore.setEnterTime(new Date());
            fcStore.setAsSupplier(0);
            fcStore.setVipLevel(3);

            LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            fcStore.setLeaderId(loginUser.getId());
            fcStore.setLeaderName(loginUser.getRealname());
            fcStore.setLeaderPhone(loginUser.getPhone());
            fcStore.setStoreManagerId(loginUser.getId());
            fcStore.setStoreManagerName(loginUser.getRealname());
            fcStore.setStoreManagerPhone(loginUser.getPhone());
            this.save(fcStore);

            // 个人信息将店主和门店绑定
            storeMapper.changeStoreForUser(fcStore.getId(), loginUser.getId());
            // 将店主加入门店雇员
            storeEmployeeService.addStoreEmployee(fcStore.getId(), loginUser.getId(), loginUser.getPhone(), "store_admin");
            // 新增一条公告信息
            storeNoticeService.addByTitle("欢迎《" + fcStore.getStoreShortName() + "》门店成功入驻成功");//
        } else {
            this.updateById(fcStore);
        }
        return this.getById(fcStore.getId());
    }

    @Override
    public StoreHotGoodsVO queryStoreHotGoodsByStoreId(String storeId) throws Exception {
        if (StringUtils.isEmpty(storeId)) throw new Exception("[storeId]不能为空");
        StoreHotGoodsVO storeHotGoodsVO = new StoreHotGoodsVO();
        StoreEntity storeEntityDB = this.getById(storeId);
        if (storeEntityDB == null) throw new Exception("店铺不存在");
        BeanUtils.copyProperties(storeEntityDB, storeHotGoodsVO);
        // 店铺热门商品
        LambdaQueryWrapper<GoodsEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsEntity::getState, GoodsEntity.STATE_ONLINE);
        queryWrapper.eq(GoodsEntity::getStoreId, storeId);
        queryWrapper.orderByDesc(GoodsEntity::getCreateTime);

        IPage<GoodsEntity> page = goodsService.page(new Page<>(1, 6), queryWrapper);
        storeHotGoodsVO.setHotGoodsList(page.getRecords());
        return storeHotGoodsVO;
    }

    @Override
    public IPage<StoreEntity> queryStoreForNearby(Page<StoreEntity> page, String locDistrict, Double locLat, Double locLng, Integer state) {
        // 附近商家
        LambdaQueryWrapper<StoreEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StoreEntity::getState, state);
        return this.page(page, queryWrapper);
    }

    @Override
    public IPage<StoreEntity> storePageForCurrUser(Page<StoreEntity> page) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return storeMapper.storePageForCurrUser(page, loginUser.getPhone());
    }

    @Override
    public StoreEntity changeStoreForUser(String storeId) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        storeMapper.changeStoreForUser(storeId, loginUser.getId());
        return this.queryStoreAllInfoByStoreId(storeId);
    }

    @Override
    public StoreVO getShowStoreInfo(String storeId) throws Exception {
        StoreEntity fcStore = this.getById(storeId);
        StoreVO storeVO = new StoreVO();
        BeanUtils.copyProperties(fcStore, storeVO);

        // 查询收藏情况
        CollectEntity collect = collectService.getCurrCollectStateByStoreId(storeId);
        storeVO.setHasCollected(collect != null);

        // 查询收藏情况
        StoreSupplierEntity storeSupplier = storeSupplierService.getStoreSupplierEntityBySupplierId(storeId);
        storeVO.setHasSupplier(storeSupplier != null);

        // 新增商品浏览记录
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        BrowserEntity goodsBrowser = new BrowserEntity();
        goodsBrowser.setStoreId(storeId);
        goodsBrowser.setGoodsId("");
        goodsBrowser.setCustomerId(loginUser.getId());
        browserMapper.insert(goodsBrowser);

        return storeVO;
    }
}
